package latin.controller.portal;

import com.google.common.collect.Lists;
import latin.common.Const;
import latin.common.ResponseCode;
import latin.common.ServiceResponse;
import latin.dao.JavaMapper;
import latin.pojo.Java;
import latin.pojo.User;
import latin.service.IJavaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/java/")
public class JavaController {

   /* @Autowired
    private JavaMapper javaMapper;*/
    @Autowired
    private IJavaService iJavaService;
    //获取10道题
    @RequestMapping( "get.do")
    @ResponseBody
    public ServiceResponse<List<Java>> getJava(HttpSession session){

        //判断用户是否登录
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
       return iJavaService.getJava();
    }

    //添加java面试题,管理员才有权限
    @RequestMapping( "add.do")
    @ResponseBody
    public ServiceResponse addJava(HttpSession session,Java java){
        //判断用户是否登录
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        if(user.getRole() == 0){
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.ILLGAL_ARGUMENT.getCode(),ResponseCode.ILLGAL_ARGUMENT.getDesc());
        }
        return iJavaService.addJava(java);
    }

}
